<template>
    <d2-container>
        <el-main>

            <el-form ref="schoolFormData" :model="schoolFormData" label-width="80px" label-position="left"
                     :rules="formRules">
                <el-form-item label="校区类型" prop="schoolType">
                    <el-radio-group v-model="schoolFormData.schoolType" :disabled="disabled || (schoolFormData.id == '5d7515647ada0f122e442b6a') || schoolTypeDisabled || headquarterfix">
                        <el-radio :label="2">分校区</el-radio>
                        <el-radio :label="3" :disabled="schoolDetail.headquarter != 1">主校区</el-radio>

                    </el-radio-group>
                </el-form-item>

                <el-form-item label="校区名称" prop="name">
                    <el-input placeholder="请输入校区名称" v-model="schoolFormData.name" :disabled="disabled || headquarterfix"></el-input>
                </el-form-item>

                <el-form-item label="支付金额" prop="payAmount">
                    <el-input placeholder="请输入金额" v-model.number="schoolFormData.payAmount" type="number"
                              :disabled="disabled || (schoolFormData.id == '5d7515647ada0f122e442b6a') || headquarterfix"></el-input>
                </el-form-item>

                <el-form-item label="支付状态" prop="payStatus">
                    <el-radio-group v-model="schoolFormData.payStatus" :disabled="disabled || (schoolFormData.id == '5d7515647ada0f122e442b6a') || headquarterfix">
                        <el-radio :label="1">已缴费</el-radio>
                        <el-radio :label="0">未交费</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item label="试用期（月）" prop="probation">
                    <el-input placeholder="请输入试用期" v-model.number="schoolFormData.probation" type="number" min="0"
                              :disabled="disabled || (schoolFormData.id == '5d7515647ada0f122e442b6a') || schoolDateFix">
                    </el-input>
                </el-form-item>


                <el-form-item label="开通期限" prop="dateRange">
                    <el-date-picker
                            v-model="schoolFormData.dateRange"
                            :disabled="disabled || (schoolFormData.id == '5d7515647ada0f122e442b6a') || schoolDateFix"
                            type="daterange"
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="校区人数限制" prop="studentLimit">
                    <el-input placeholder="请输入校区人数限制" v-model.number="schoolFormData.studentLimit" type="number"
                              :disabled="disabled || headquarterfix">
                    </el-input>
                </el-form-item>

                <el-form-item label="校区地址" prop="address">
                    <el-input placeholder="请输入校区地址" v-model="schoolFormData.address" :disabled="disabled || headquarterfix"></el-input>
                </el-form-item>

                <el-form-item label="校区电话" prop="telephone">
                    <el-input placeholder="请输入校区电话" v-model="schoolFormData.telephone" :disabled="disabled || headquarterfix"></el-input>

                </el-form-item>

                <el-form-item label="校区联系人">
                    <el-input placeholder="请输入校区联系人" v-model="schoolFormData.contact"
                              :disabled="disabled || headquarterfix"></el-input>
                </el-form-item>
            </el-form>
        </el-main>
        <el-footer>
            <el-button type="primary" @click="submit">提交</el-button>
            <el-button @click="resetForm('parentsForm')" >重置</el-button>
            <el-button @click="edit" v-show="editShow">修改</el-button>
        </el-footer>
    </d2-container>
</template>

<script>
    // import { request } from "http";
    import request from "@/plugin/axios";
    import {mapState} from "vuex";
    export default {
        data() {
            return {
                schoolFormData: {
                    schoolType: undefined,
                    name: undefined,
                    payAmount: undefined,
                    payStatus: undefined,
                    probation: undefined,
                    startDate: undefined,
                    studentLimit: undefined,
                    address: undefined,
                    telephone: undefined,
                    contact: undefined,
                    deadline: undefined,
                    dateRange: '',
                    initialPayStatus:undefined
                },
                schoolDetail : undefined,
                schoolDateFix : false,
                editShow: false,
                disabled: false,
                id: undefined,
                schoolTypeDisabled: false,

                formRules: {
                    name: [
                        {required: true, message: '请输入校区名称', trigger: 'blur'},
                        {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
                    ],
                    studentId: [
                        {required: true, message: '请输入学号', trigger: 'change'}
                    ],
                    gender: [
                        {required: true, message: '请选择学生性别', trigger: 'change'}
                    ],
                    age: [
                        {required: true, message: '请输入家长年龄', trigger: 'change'},
                        {type: 'number', min: 5, max: 100, message: '年龄范围在5-100之间', trigger: 'change'}

                    ],
                    school: [
                        {required: true, message: '请选择所在校区', trigger: 'change'}
                    ],
                    // address: [
                    //     {required: true, message: '请输入校区地址', trigger: 'change'}
                    // ],
                    payType: [
                        {required: true, message: '请选择付费类型', trigger: 'change'}
                    ],

                    payStatus: [
                        {required: true, message: '请选择支付状态', trigger: 'change'}
                    ],
                    schoolType: [
                        {required: true, message: '请选择校区类型', trigger: 'change'}
                    ],
                    cellphone: [
                        {required: true, message: '手机号不能为空', trigger: 'change'},
                        {type: 'number', message: '请输入11位合法手机号', trigger: 'change'}
                    ],
                    // telephone: [
                    //     {required: true, message: '手机号不能为空', trigger: 'change'},
                    // ],
                    birthday: [
                        {type: 'date', required: true, message: '请选择出生日期', trigger: 'blur'}
                    ],
                    dateRange: [
                        {type: 'date', required: true, trigger: 'blur'}
                    ],
                    payAmount: [
                        {required: true, message: '输入金额不能为空', trigger: 'change'},
                        {type: 'number', min: 0, message: '请输入合法金额', trigger: 'change'}

                    ],
                    probation: [
                        {required: true, message: '试用期不能为空', trigger: 'change'},
                        {type: 'number', min: 1, message: '请输入合法数字', trigger: 'change'}

                    ],
                    studentLimit: [
                        {required: true, message: '学生限制人数不能为空', trigger: 'change'},
                        {type: 'number', min: 1, message: '请输入合法人数', trigger: 'change'}

                    ],
                },
            };
        },
            computed: {
            ...mapState('d2admin/user', ['info']),
        },
        methods: {
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            submit() {
                let data = {};
                let url = "/school/addSchool"
                if (this.id != undefined) {
                    data.school = this.id
                    url = "/school/updateSchool"
                }
                let createSchoolOrder = 0
                if(this.initialPayStatus == 0&&this.schoolFormData.payStatus ==1){
                    this.$confirm('是否创建校区缴费记录用作财务报表计算?', '提示', {
                    confirmButtonText: '创建',
                    cancelButtonText: '不创建',
                    type: 'warning'
                    }).then(() => {
                        (data.createSchoolOrder =1)
                        (data.schoolType = this.schoolFormData.schoolType),
                        (data.name = this.schoolFormData.name),
                        (data.payAmount = this.schoolFormData.payAmount),
                        (data.payStatus = this.schoolFormData.payStatus),
                        (data.probation = this.schoolFormData.probation),
                        (data.startDate = this.schoolFormData.dateRange[0]),
                        (data.studentLimit = this.schoolFormData.studentLimit),
                        (data.address = this.schoolFormData.address),
                        (data.telephone = this.schoolFormData.telephone),
                        (data.contact = this.schoolFormData.contact),
                        (data.deadline = this.schoolFormData.dateRange[1]),
                        (data.dateRange = this.schoolFormData.dateRange);

                    request({
                        url: url,
                        method: "post",
                        data
                    }).then(res => {
                        if (res.code == 200) {
                            this.$router.push({path: "/school-list"})
                            this.$alert("", "提交成功", {
                                confirmButtonText: "确定"
                            });
                        }
                        else if (res.code == 202){
                            let msg = '超过学校允许人数上线, 现有' + res.current + '名学生,最多' + res.total + '名学生'
                        console.log(msg,'ds')
                           this.$message({
                                showClose: true,
                                message: msg,
                                type: 'error'
                            });
                        }
                        // 返回数据
                    });
                    }).catch(() => {    
                    (data.schoolType = this.schoolFormData.schoolType),
                    (data.name = this.schoolFormData.name),
                    (data.payAmount = this.schoolFormData.payAmount),
                    (data.payStatus = this.schoolFormData.payStatus),
                    (data.probation = this.schoolFormData.probation),
                    (data.startDate = this.schoolFormData.dateRange[0]),
                    (data.studentLimit = this.schoolFormData.studentLimit),
                    (data.address = this.schoolFormData.address),
                    (data.telephone = this.schoolFormData.telephone),
                    (data.contact = this.schoolFormData.contact),
                    (data.deadline = this.schoolFormData.dateRange[1]),
                    (data.dateRange = this.schoolFormData.dateRange);

                request({
                    url: url,
                    method: "post",
                    data
                }).then(res => {
                    if (res.code == 200) {
                        this.$router.push({path: "/school-list"})
                        this.$alert("", "提交成功", {
                            confirmButtonText: "确定"
                        });
                    }
                    // 返回数据
                });  
                        });
                    }else{
                    (data.schoolType = this.schoolFormData.schoolType),
                    (data.name = this.schoolFormData.name),
                    (data.payAmount = this.schoolFormData.payAmount),
                    (data.payStatus = this.schoolFormData.payStatus),
                    (data.probation = this.schoolFormData.probation),
                    (data.startDate = this.schoolFormData.dateRange[0]),
                    (data.studentLimit = this.schoolFormData.studentLimit),
                    (data.address = this.schoolFormData.address),
                    (data.telephone = this.schoolFormData.telephone),
                    (data.contact = this.schoolFormData.contact),
                    (data.deadline = this.schoolFormData.dateRange[1]),
                    (data.dateRange = this.schoolFormData.dateRange);

                request({
                    url: url,
                    method: "post",
                    data
                }).then(res => {
                    if (res.code == 200) {
                        this.refresh = true
                        this.schoolFormData = {
                            schoolType: undefined,
                    name: undefined,
                    payAmount: undefined,
                    payStatus: undefined,
                    probation: undefined,
                    startDate: undefined,
                    studentLimit: undefined,
                    address: undefined,
                    telephone: undefined,
                    contact: undefined,
                    deadline: undefined,
                    dateRange: '',
                    initialPayStatus:undefined
                        }
                        this.$router.push({path: "/school-list"})
                        this.$alert("", "提交成功", {
                            confirmButtonText: "确定"
                        });
                    }
                    else if (res.code == 201){
                        this.$alert(res.message)
                    }
                    // 返回数据
                });
                }
            },
            edit() {
                this.disabled = false
            },
            getSchoolOne(id) {
                request({
                    url: "/school/getSchoolOne",
                    method: "post",
                    data: {id: id}
                }).then(res => {
                    if(res.data.payStatus == 0){
                        this.initialPayStatus =0
                    }
                    if (this.school[0].headquarter == 2){
                        this.editShow = false
                    }
                    else if (this.school[0].headquarter == 1){
                        this.editShow = true
                    }
                    this.originType = res.data.schoolType
                    console.log(res.data.schoolType,'rrr',this.originType,this.originType== 3)
                    if (this.originType == 3){
                        this.schoolTypeDisabled = true
                    }
                    
                    this.schoolFormData.schoolType = res.data.schoolType,
                        this.schoolFormData.id = res.data._id,
                        this.schoolFormData.name = res.data.name,
                        this.schoolFormData.payAmount = res.data.payAmount,
                        this.schoolFormData.payStatus = res.data.payStatus,
                        this.schoolFormData.probation = res.data.probation,
                        this.schoolFormData.startDate = res.data.startDate,
                        this.schoolFormData.studentLimit = res.data.studentLimit,
                        this.schoolFormData.address = res.data.address,
                        this.schoolFormData.telephone = res.data.telephone,
                        this.schoolFormData.contact = res.data.contact,
                        this.schoolFormData.deadline = res.data.deadline,
                        this.schoolFormData.dateRange = [res.data.startDate, res.data.deadline]

                });
            }
        },

         beforeRouteEnter(to,from,next){
            if (to.meta.keep){
                to.meta.keep = undefined
                next(v=>{
                  v.schoolFormData = to.meta.schoolFormData
                  to.meta.schoolFormData = undefined
                  to.meta.keep = undefined
                })
            }
            else{
                next()
            }
            next()

        },
        beforeRouteLeave(to,from,next){
          if (this.$route.query.id == undefined) {
            from.meta.keep = true
            from.meta.schoolFormData = this.schoolFormData      
          }
          if (this.refresh){
            to.meta.refresh = 'a'
          }

          next()
        },

        created() {

            this.school = this.info.school[0]._id
            console.log(this.school)
            this.schoolDetail = this.info.school[0]

            if (this.schoolDetail.headquarter == 2){
                        if (this.$route.query.id == this.schoolDetail._id){
                            this.headquarterfix = true
                        }
                        this.schoolDateFix = true
                        this.schoolFormData.probation = this.schoolDetail.probation
                        this.schoolFormData.startDate = this.schoolDetail.startDate
                        this.schoolFormData.deadline = this.schoolDetail.deadline
                        this.schoolFormData.dateRange = [this.schoolDetail.startDate, this.schoolDetail.deadline]
                    
                
            }

            if (this.$route.query.id != undefined) {
                this.editShow = true    
                // this.schoolTypeDisabled = true

                this.disabled = true
                this.id = this.$route.query.id
                this.getSchoolOne(this.$route.query.id)
            }
        }
    };
</script>
